'use strict';

const Controller = require('egg').Controller;

class HttpClientController extends Controller {
  async index() {
    const { ctx } = this;
    const url = 'http://ip.360.cn/IPShare/info';
    const result = await ctx.curl(url,{
      dataType: 'json',
      // data: {
      //   portal: 2,
      //   lang: 'zh-CN',
      //   country: 'CN'
      // }
    });
    ctx.body = {
      result: result
    }
  };
  async indexPost() {
    const { ctx } = this;
    // 获取post 请求参数
    let count = ctx.request.body.count;
    const url = 'https://route.showapi.com/1211-1';
    const result = await ctx.curl(url,{
      method: 'POST',
      dataType: 'json',
      // contentType: 'json',
      data: {
        count: count,
        showapi_timestamp: this.formatterDateTime(),
        showapi_appid: '301411',
        showapi_sign: 'a73c47bfe35140569ff01c65d3d91c04'
      } 
    });
    ctx.body = {
      result: result
    }
  };
  formatterDateTime() {
    var date=new Date()
    var month=date.getMonth() + 1
    var datetime = date.getFullYear()
      + ""// "年"
      + (month >= 10 ? month : "0"+ month)
      + ""// "月"
      + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
      + ""
      + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())
      + ""
      + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
      + ""
      + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
    return datetime;
  }
}

module.exports = HttpClientController;
